﻿using AutoMapper;
using MediatR;
using SqlSugar;
using Warehouse.Domain;
using Warehouse.ErrorCord;
using Warehouse.Write.Api.Actions.Warehouse.Command;

namespace Warehouse.Write.Api.Actions.Warehouse.CommandHandle
{
    public class UpdCategoryCommandHandel : IRequestHandler<UpdCategoryCommand, APIResult<Domain.WarehouseCategory>>
    {
        private readonly ISqlSugarClient db;
        private readonly IMapper mapper;

        public UpdCategoryCommandHandel(ISqlSugarClient db, IMapper mapper)
        {
            this.db = db;
            this.mapper = mapper;
        }

        public async Task<APIResult<Domain.WarehouseCategory>> Handle(UpdCategoryCommand request, CancellationToken cancellationToken)
        {
            APIResult<Domain.WarehouseCategory> result = new APIResult<Domain.WarehouseCategory>();
            var res = mapper.Map<Domain.WarehouseCategory>(request);
            var count = await db.Updateable<Domain.WarehouseCategory>(res).ExecuteCommandAsync();
            if (count < 0)
            {
                result.Code = APIEnum.Fail;
                result.Msg = "修改失败";
                return await Task.FromResult(result);
            }
            result.Code = APIEnum.Success;
            result.Msg = "修改成功";
            return await Task.FromResult(result);
        }
    }
}
